package halberd.filter;

import halberd.DataType;
import halberd.ImgFilter;
import halberd.data.ImgData;
import halberd.data.ImgDataFactory;

public class ChessboardFilter implements ImgFilter {
	private int scale=10;
	public ChessboardFilter(int scale) {
		if(scale<10) {
			return;
		}
		this.scale=scale;
	}
	public ImgData process(ImgData input) {
		if(!canProcess(input)) {
			return null;
		}
		ImgData result=ImgDataFactory.createImgData(input.getWidth(), input.getHeight());
		chessboard(input.getDatas(),result.getDatas(),input.getWidth(),input.getHeight());
		result.setDataType(DataType.GRAY);
		return result;
	}
	private boolean canProcess(ImgData input) {
		if(input==null) {
			return false;
		}
		DataType dt=input.getDataType();
		if(dt==DataType.BINARY) {
			return true;
		}
		return false;
	}
	private void chessboard(int[][][] src,int[][][] dst,int w,int h) {
		
		for (int i = 0; i < w; i++)
	    {
			int m=i/scale;
	        for (int j = 0; j < h; j++)
	        {
				int n=j/scale;
				if((m%2==0&&n%2==0)||(m%2==1&&n%2==1))
				{
					dst[i][j][4]=(255-src[i][j][4]);
				}else {
					dst[i][j][4]=src[i][j][4];
				}
			}
		}

	}
}
